package com.park.web.controller.user;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.park.client.model.user.UserModel;
import com.park.client.service.user.UserService;
import com.park.common.constant.Constant;
import com.park.common.util.Result;
import com.park.web.contant.Global;
import com.park.web.controller.BaseController;
import com.park.web.util.SessionUtil;

/**
 * @author Administrator
 *账号管理
 */
@Controller
@RequestMapping("/user/account")
public class UserAccountController extends BaseController{
	@Autowired
	private UserService userService;
	/**
	 * 邮箱设置 ,是否订阅
	 */
	@RequestMapping("accountManage")
	public String accountManage(HttpServletRequest request,HttpSession session){
		UserModel user = userService.selectByPrimaryKey(SessionUtil.getUserID(session));
		request.setAttribute("user", user);
		return Global.USER_PATH+"account/accountManage";
	}
	
	/**
	 * 个人资料修改页面
	 */
	@RequestMapping("personData")
	public String personData(HttpServletRequest request,HttpSession session){
		return Global.USER_PATH+"account/personData";
	}
	
	/**
	 * 扫码关注
	 */
	@RequestMapping("sweepCode")
	public String sweepCode(HttpServletRequest request,HttpSession session){
		return Global.USER_PATH+"account/sweepCode";
	}
	
	/**
	 * 个人资料修改页面
	 */
	@RequestMapping("personDataSave")
	@ResponseBody
	public Result<Boolean> personDataSave(UserModel user,HttpServletRequest request,HttpSession session){
		Result<Boolean> obj = new Result<>();
		//判断界面传过来的用户名和登陆者是否匹配
		UserModel u = SessionUtil.getSessionUser(session);
		user.setId(u.getId());
		if(!user.getUsername().equals(u.getUsername())){
			obj.setMessage(Constant.IS_NOT_EQUAL_LOGINNAME);
			obj.setSuccess(false);
			return obj;
		}
		user.setUsername(null);
		boolean success =  userService.updateByPrimaryKeySelective(user);
		if(success){
			obj.setMessage(Constant.SUCCESS_MSG);
		}else{
			obj.setMessage(Constant.FAIL_MSG);
		}
		obj.setSuccess(success);
		return obj;
	}
	
	/**
	 *消息设置页面
	 */
	@RequestMapping("messageSet")
	public String messageSet(HttpSession session,HttpServletRequest request){
		UserModel user = userService.selectByPrimaryKey(SessionUtil.getUserID(session));
		request.setAttribute("user", user);
		return  Global.USER_PATH+"account/messageSet";
	}
	
	/**
	 *消息保存页面
	 */
	@RequestMapping("messageSave")
	@ResponseBody
	public Result<Boolean> messageSave(UserModel user,HttpSession session,HttpServletRequest request){
		Result<Boolean> obj = new Result<>();
		user.setId(SessionUtil.getUserID(session));
		boolean success =  userService.updateByPrimaryKeySelective(user);
		if(success){
			obj.setMessage(Constant.SUCCESS_MSG);
		}else{
			obj.setMessage(Constant.FAIL_MSG);
		}
		obj.setSuccess(success);
		return obj;
	}
	
}
